package com.rongzhe.house.presenter;

import android.content.Intent;
import android.net.Uri;

import com.rongzhe.house.entity.voo.User;
import com.rongzhe.house.entity.voo.UserAuthInfo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.LoginActivity;
import com.rongzhe.house.ui.activity.UserInfoActivity;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.util.ArrayList;

import me.iwf.photopicker.PhotoPicker;

import static android.app.Activity.RESULT_OK;

/**
 * Created by huang on 2017/11/20.
 */

public class UserInfoPresenter extends BasePresenter {
    public UserInfoPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        loadUserInfo();
        loadUserAuthInfo();
    }

    private void loadUserAuthInfo() {
        UserManager.getInstance().getAuthInfo(new DataListener<UserAuthInfo>() {
            @Override
            public void onSuccess(UserAuthInfo data, String message) {
                ((UserInfoActivity) getUiControlInterface()).setIsAuthed(data == null ? 0 : data.getState());
            }

            @Override
            public void onFailed(Throwable error, String message) {

            }

            @Override
            public void onEnd() {

            }
        });
    }

    private void loadUserInfo() {
        UserManager.getInstance().getUserInfo(new DataListener<User>() {
            @Override
            public void onSuccess(User data, String message) {
                ((UserInfoActivity) getUiControlInterface()).showUserInfo(data);
            }

            @Override
            public void onFailed(Throwable error, String message) {

            }

            @Override
            public void onEnd() {

            }
        });
    }

    public void startChooseImage() {
        PhotoPicker.builder()
                .setPhotoCount(1)
                .setShowCamera(true)
                .setShowGif(false)
                .setPreviewEnabled(false)
                .start((UserInfoActivity) getUiControlInterface(), PhotoPicker.REQUEST_CODE);
    }

    @Override
    public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                Uri uri = Uri.fromFile(new File(photos.get(0)));
                UCrop.of(uri, uri)
                        .withAspectRatio(9, 9)
                        .withMaxResultSize(360, 360)
                        .start((UserInfoActivity) getUiControlInterface());
            }
        }
        if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
            final Uri resultUri = UCrop.getOutput(data);
            uploadHeadImage(resultUri);
            System.out.println(resultUri);
        } else if (resultCode == UCrop.RESULT_ERROR) {
            final Throwable cropError = UCrop.getError(data);
        }
        return true;
    }


    public void uploadHeadImage(Uri resultUri) {
        getUiControlInterface().showProgress("uploading....");
        UserManager.getInstance().uploadHeadImage(mContext, resultUri,
                new DataListener<String>() {
                    @Override
                    public void onSuccess(String data, String message) {
                        getUiControlInterface().showToast("success");
                        ((UserInfoActivity) getUiControlInterface()).setImgHead(data);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                    }

                    @Override
                    public void onEnd() {
                        getUiControlInterface().dismissProgress();
                    }
                });
    }

    public void logout() {
        UserManager.getInstance().cleanLogin();
        //TODO need return to main page
        getUiControlInterface().finish();
        Intent intent = new Intent(mContext, LoginActivity.class);
        getUiControlInterface().launchActivity(false, intent);
    }
}
